package com.chaos.payment.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import com.chaos.payment.entity.TPayFlow;
import com.chaos.payment.mapper.TPayFlowMapper;
import com.chaos.order.api.data.TPayFlowData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
*
* @author yjp
* @since 2024-06-18
*/
@Slf4j
@Component
public class TPayFlowService extends ServiceImpl<TPayFlowMapper, TPayFlow> {

	public ID insertModel(TPayFlowData data) {
		TPayFlow entity = ConvertHelper.convert(data, TPayFlow.class);
		return save(entity) ? new ID(entity.getId()) : null;
	}

	public boolean deleteModel(ID data) {
		return removeById(data.getId());
	}

	public boolean updateModelByID(TPayFlowData data) {
		TPayFlow entity = ConvertHelper.convert(data, TPayFlow.class);
		return update(entity, new UpdateWrapper<TPayFlow>().eq(Table.ID, data.getId()));
	}

	public TPayFlowData selectByID(ID data) {
		return ConvertHelper.convert(getOne(new QueryWrapper<TPayFlow>().eq(Table.ID, data.getId())), TPayFlowData.class);
	}

	public TPayFlow selectByOrderNo(String orderNo) {
		return getOne(Wrappers.<TPayFlow>lambdaQuery().eq(TPayFlow::getOrderNo, orderNo));
	}

	public TPayFlow getBytThirdOrderNo(String thirdOrderNo) {
		return getOne(Wrappers.<TPayFlow>lambdaQuery().eq(TPayFlow::getThirdOrderNo, thirdOrderNo));
	}

	public List<TPayFlowData> selectByData(TPayFlowData data) {
		QueryWrapper<TPayFlow> query = new QueryWrapper();
		query.orderByDesc(Table.ID);
		return ConvertHelper.convert(list(query), TPayFlowData.class);
	}

	public PageList<TPayFlowData> selectByPage(PageQueryDto<TPayFlowData> pageData) {
		QueryWrapper<TPayFlow> query = new QueryWrapper();
		query.orderByDesc(Table.ID);
		return PageHelper.pageList(page(PageHelper.page(pageData), query), TPayFlowData.class);
	}

	public TPayFlow selectFlow(TPayFlowData flowData) {
		return getOne(Wrappers.<TPayFlow>lambdaQuery()
				.eq(StrUtil.isNotBlank(flowData.getOrderNo()),TPayFlow::getOrderNo, flowData.getOrderNo())
				.eq(StrUtil.isNotBlank(flowData.getThirdOrderNo()),TPayFlow::getThirdOrderNo, flowData.getThirdOrderNo())
				.eq(StrUtil.isNotBlank(flowData.getThirdFlowNo()),TPayFlow::getThirdFlowNo, flowData.getThirdFlowNo())
				.eq(Objects.nonNull(flowData.getPayType()),TPayFlow::getPayType, flowData.getPayType())
				.eq(Objects.nonNull(flowData.getPayType()),TPayFlow::getPayTradeType, flowData.getPayTradeType())
				.eq(TPayFlow::getUserId, flowData.getUserId())
		);
	}
}
